var userEditParams = {
    action: {
        addUser: "sysUser/insertUserVo",
        findRoleName: "role/selectRoleName"
    }
}

function findRoleName() {
    requestUtilParams.xhrGet(userEditParams.action.findRoleName,{async:false}).done(function (res) {
        $("#roleGroup").empty();
        var list=res.data;
        for(var i=0;i<list.length;i++){
            $("#roleGroup").append("<input type='checkbox' title='"+list[i].name+"' value='"+list[i].role_id+"'>")
        }
        layui.use("form",function () {
            var form=layui.form;
            form.render();
        });
    })
}



function checkData() {
    var str="";
    var account=$("#account-user-submit").val();
    var name=$("#name-user-submit").val();
    if(account==""){
        str+='用户名不能为空\n';
    }
    if(name==""){
        str+='姓名不能为空\n';
    }
    if(str.length>0){
        alert(str);
        return false;
    }else {
        var num=0;
        $("roleGroup input:checked").each(function () {
            $(this).attr("name","listUserRole["+num+"].fk_role_id");
            num++;
        });
        return true;
    }
}



function submitAddUser() {
    if(checkData()){
        var data = $("#formSubmitUser").serialize();
        // requestUtilParams.xhr(userEditParams.action.addUser, data).done(function (res) {
        requestUtilParams.xhrUpload(userEditParams.action.addUser,"#formSubmitUser").done(function (res) {
            alert(res.message);
            $("#formSubmitUser")[0].reset();
        })
    }
}


function xhrUpload(actionName,formNode){
    var data=new FormData($(formNode)[0]);
    $(formNode).attr("enctype","multipart/form-data");
    data.append("token",requestUtilParams.getToken());
    var xhrReq = $.ajax({
        url: requestUtilParams.host + actionName,
        type: "post",
        contentType:false,
        processData:false,
        data: data,
        dataType: "json",
        success: function (res) {

        }, error: function (res, status, xr) {
            if (res.responseJSON != null) {
                console.log("请求失败：" + res.responseJSON.message);
                alert(res.responseJSON.message)
            } else {
                alert("请求失败");
            }
        }, complete: function (res) {
            alert("请求完成");
            requestUtilParams.getToken(1);
        }
    });
    return xhrReq;
}

$(function () {
    findRoleName();
    $("#btnAddUser").click(function () {
        submitAddUser();
    });
    new uploadPreview({
        UpBtn:"tempFile",
        DivShow:"imgGroup",
        ImgShow:"imgPreview"
    })
})